package com.turnengine.client.local.action.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.action.bean.ActionConditionSerializer;
import com.turnengine.client.local.action.bean.IActionCondition;
import com.turnengine.client.local.action.enums.ActionConditionExecute;
import com.turnengine.client.local.action.enums.ActionConditionMandatory;
import com.turnengine.client.local.action.enums.ActionConditionType;
import com.turnengine.client.local.action.enums.ActionTargetType;
import java.io.IOException;

/**
 * The Add Action Condition Data Serializer.
 */
public class AddActionConditionDataSerializer extends AbstractCommandDataSerializer<IActionCondition> implements IAddActionConditionDataSerializer {

	/**
	 * Creates a new Add Action Condition Data Serializer.
	 */
	public AddActionConditionDataSerializer() {
		super(AddActionCondition.COMMAND_ID);
	}

	@Override
	public IActionCondition readReturnValue(IDataReader reader) throws IOException {
		IActionCondition value = new ActionConditionSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IActionCondition value) throws IOException {
		new ActionConditionSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IActionCondition> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int actionId = reader.readInt();
		ActionTargetType targetType = reader.readEnum(ActionTargetType.class, true);
		ActionConditionType type = reader.readEnum(ActionConditionType.class, true);
		ActionConditionExecute execute = reader.readEnum(ActionConditionExecute.class, true);
		ActionConditionMandatory mandatory = reader.readEnum(ActionConditionMandatory.class, true);
		int conditionId = reader.readInt();
		long conditionAmount = reader.readLong();

		// Create object
		AddActionCondition object = new AddActionCondition();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setActionId(actionId);
		object.setTargetType(targetType);
		object.setType(type);
		object.setExecute(execute);
		object.setMandatory(mandatory);
		object.setConditionId(conditionId);
		object.setConditionAmount(conditionAmount);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IActionCondition> argument) throws IOException {
		AddActionCondition object = (AddActionCondition) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int actionId = object.getActionId();
		ActionTargetType targetType = object.getTargetType();
		ActionConditionType type = object.getType();
		ActionConditionExecute execute = object.getExecute();
		ActionConditionMandatory mandatory = object.getMandatory();
		int conditionId = object.getConditionId();
		long conditionAmount = object.getConditionAmount();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeInt(actionId);
		writer.writeEnum(targetType, true);
		writer.writeEnum(type, true);
		writer.writeEnum(execute, true);
		writer.writeEnum(mandatory, true);
		writer.writeInt(conditionId);
		writer.writeLong(conditionAmount);
	}

	@Override
	public IExecutableCommandResponse<IActionCondition> newResponse() {
		return new AddActionConditionResponse();
	}
}
